// 所有Node.js API都可以在预加载过程中使用。
// 它拥有与Chrome扩展一样的沙盒。
const { ipcRenderer, contextBridge } = require('electron');

/*
  渲染进程不能直接使用 主进程 api，在这里把 使用ipcRenderer 暴露给渲染进程
  ipcRenderer.send对应的是主线程的ipcMain.on
  ipcRenderer.invoke对应的是主线程的ipcMain.handle
 */
contextBridge.exposeInMainWorld('api', {
    closeWindow: () => {
        ipcRenderer.send('close-main-window');
    },
    minimizeWindow: () => {
        ipcRenderer.send('minimize-main-window');
    },
    maximizeWindow: () => {
        ipcRenderer.send('maximize-main-window');
    },
    unmaximizeWindow: () => {
        ipcRenderer.send('unmaximize-main-window');
    },
    isMaximizeStatus: async () => {
        return await ipcRenderer.invoke('is_maximize_status');
    },
    process: async () => {
        return process;
    },
    moveWin: (pos) => {
        ipcRenderer.send('move-win', pos);
    },
    __dirname: async () => {
        return await ipcRenderer.invoke('__dirname');
    },
});

contextBridge.exposeInMainWorld('download', {
    downloadsPath: async () => {
        return await ipcRenderer.invoke('downloads_path');
    },
    downloadFile: async (pos) => {
        return await ipcRenderer.invoke('download-file', pos.url, pos.filename);
    },
    cancelDownload: () => {
        ipcRenderer.send('cancel-download');
    },
    sysUpdateInstall: (installPath) => {
        ipcRenderer.send('sys_update_install', installPath);
    }
});

// 监听来自主进程的消息
ipcRenderer.on('window-state-changed', (event, newState) => {
    // 这里发送事件，由VUE页面监听
    window.dispatchEvent(new CustomEvent('window-state-change', { detail: newState }));
});
ipcRenderer.on('download-progress', (event, progress) => {
    // 这里发送事件，由VUE页面监听
    window.dispatchEvent(new CustomEvent('download-progress', { detail: progress }));
});
ipcRenderer.on('download-completed', (event, downloadPath) => {
    // 这里发送事件，由VUE页面监听
    window.dispatchEvent(new CustomEvent('download-completed', { detail: downloadPath }));
});
// 监听msg
ipcRenderer.on('msg', (event, msg) => {
    // 这里发送事件，由VUE页面监听
    window.dispatchEvent(new CustomEvent('msg', { detail: msg }));
});

// 监听msg
ipcRenderer.on('upload-result', (event, result) => {
    // 这里发送事件，由VUE页面监听
    window.dispatchEvent(new CustomEvent('upload-result', {detail:result}));
});
// 监听status
ipcRenderer.on('status', (event, msg) => {
    // 这里发送事件，由VUE页面监听
    window.dispatchEvent(new CustomEvent('status', { detail: msg }));
});


contextBridge.exposeInMainWorld('base', {
    createMaterial: async (form) => {
        return ipcRenderer.invoke('createMaterial', form);
    },
    updateMaterial: async (form) => {
        return ipcRenderer.invoke('updateMaterial', form);
    },
    removeMaterial: async (form) => {
        return ipcRenderer.invoke('removeMaterial', form);
    },

    tableMaterial: async (query) => {
        console.log("222",query)
        return ipcRenderer.invoke('tableMaterial', query);
    },
    listMaterial: async (query) => {
        console.log("333",query)
        return ipcRenderer.invoke('listMaterial', query);
    },
    createCustomer: async (form) => {
        return ipcRenderer.invoke('createCustomer', form);
    },
    updateCustomer: async (form) => {
        return ipcRenderer.invoke('updateCustomer', form);
    },
    removeCustomer: async (form) => {
        return ipcRenderer.invoke('removeCustomer', form);
    },

    tableCustomer: async (query) => {
        return ipcRenderer.invoke('tableCustomer', query);
    },
    listCustomer: async (query) => {
        return ipcRenderer.invoke('listCustomer', query);
    },

    selectFile: async (params) => {
        return await ipcRenderer.invoke('select-file',params);
    },
    selectImportFile: async (params) => {
        return await ipcRenderer.invoke('select-import-file',params);
    },

   
    saveFile: async (params) => {
        ipcRenderer.send('saveFile',params);
    },
    parseExcel: async (params) =>{
        return await ipcRenderer.invoke('parse-excel',params);  
    },
    generateExcel: async (params) =>{
        console.log(333,params)
        return await ipcRenderer.invoke('generate-excel',params);  
    },

    openVirtualKeyboard: () => {
        console.log("发出开启虚拟键盘请求")
        ipcRenderer.send('open-virtual-keyboard');
    },
    closeVirtualKeyboard: () => {
        console.log("发出关闭虚拟键盘请求")
        ipcRenderer.send('close-virtual-keyboard');
    },

})

window.addEventListener('DOMContentLoaded', () => {
    const replaceText = (selector, text) => {
        const element = document.getElementById(selector)
        if (element) element.innerText = text
    }

    // for (const dependency of ['chrome', 'node', 'electron']) {
    //     replaceText(`${dependency}-version`, process.versions[dependency])
    // }
})



